/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.tigris.jasmincc.framework;

import java.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <i>RegexRegion<i> might be one of the TOKEN, MORE, SKIP or SPECIAL_TOKEN.
 * 
 * It is in the syntax of
 * <p> ([ LOOKAHEAD(2) "<" "*" ">"
 * <p> | "<" <IDENTIFIER> ( "," <IDENTIFIER> )* ">" ])
 * <p>
 * regexpr_kind() [ "[" "IGNORE_CASE" "]" ] ":"
 * <p>
 * "{" regexpr_spec() ( "|" regexpr_spec() )* "}"
 * <p>
 * <p>
 * 
 * @author Sam Huang
 * 
 */
public class RegexRegion {

	protected String regexRegionType;

	protected Log logger = LogFactory.getLog(getClass());

	protected List<String> headers;

	protected List<RegexSpec> regexSpecs;

	protected boolean isIgnoreCase;

	public RegexRegion() {
		headers = new ArrayList<String>();
		regexSpecs = new ArrayList<RegexSpec>();
	}

	/**
	 * @return the headers
	 */
	public List<String> getHeaders() {
		return headers;
	}

	/**
	 * @param headers
	 *            the headers to set
	 */
	public void setHeaders(List<String> headers) {
		this.headers = headers;
	}

	/**
	 * @param headers
	 *            the headers to set
	 */
	public void addHeader(String header) {
		this.headers.add(header);
	}

	/**
	 * @return the isIgnoreCase
	 */
	public boolean isIgnoreCase() {
		return isIgnoreCase;
	}

	/**
	 * @param isIgnoreCase
	 *            the isIgnoreCase to set
	 */
	public void setIgnoreCase(boolean isIgnoreCase) {
		this.isIgnoreCase = isIgnoreCase;
	}

	/**
	 * @return the regex
	 */
	public List<RegexSpec> getRegex() {
		return regexSpecs;
	}

	/**
	 * @param regex
	 *            the regex to set
	 */
	public void setRegex(List<RegexSpec> regex) {
		this.regexSpecs = regex;
	}

	public void addRegexSpec(RegexSpec regexSpec) {
		this.regexSpecs.add(regexSpec);
	}

	/**
	 * @return the regexRegionType
	 */
	public String getRegexRegionType() {
		return regexRegionType;
	}

	/**
	 * @param regexRegionType
	 *            the regexRegionType to set
	 */
	public void setRegexRegionType(String regexRegionType) {
		this.regexRegionType = regexRegionType;
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();

		if (headers.size() > 0) {
			buffer.append("<");
			for (Iterator<String> iterator = headers.iterator(); iterator
					.hasNext();) {
				buffer.append(iterator.next());
				if (iterator.hasNext()) {
					buffer.append(", ");
				}
			}
			buffer.append(">");
		}
		buffer.append(this.regexRegionType);
		if (this.isIgnoreCase) {
			buffer.append("IGNORE_CASE");
		}
		buffer.append(":{\n");
		for (Iterator<RegexSpec> iterator = this.regexSpecs.iterator(); iterator
				.hasNext();) {
			RegexSpec rs = iterator.next();
			buffer.append("\t");
			if (rs.getValue() != null) {
				buffer.append("<");
			}
			buffer.append(rs.getName());
			if (rs.getValue() != null) {
				buffer.append(":").append(rs.getValue());
				buffer.append(">");
			}
			if (rs.getBlock() != null) {
				buffer.append(rs.getBlock());
			}
			if (rs.getToken() != null) {
				buffer.append(":").append(rs.getToken());
			}
			if (iterator.hasNext()) {
				buffer.append("\t|\n");
			}
		}
		buffer.append("}\n");
		return buffer.toString();
	}
}
